/*
ok1053w_io.c
offer washer service board io config and driver
designed by lunanting

------------------------------
2020-05-24 15:42:22
create file
------------------------------
*/
#include "../../include/aos/hal/gpio.h"
#ifdef OK1054W
#include "../../board/ok1054w/board/board.h"
#elif defined OK1054E
#include "../../board/ok1054e/board/board.h"
#else
#include "../../board/ok1053w/board/board.h"
#endif
#include "../bsp/cli_cmd.h"
#include "string.h"
#include "aos/kernel.h"
#include "../bsp/cli_cmd.h"



static int xp_boardIO_debug(char *type,char *fun,char *param);


/*
board  io init
param:
    default_sta: default state
return:
    0=success
*/
int xp_boardIO_init(int default_sta){
    gpio_dev_t gpio;
    static int init=0;
    int32_t (*fun)(gpio_dev_t *);
    int sta=0;

    if(init==1)return 0;
    if(default_sta==0){
       fun=hal_gpio_output_low;
    }
    else{
        fun=hal_gpio_output_high;
    }
    gpio.config=OUTPUT_PUSH_PULL;
    gpio.priv=NULL;
    for(int i=0;i<4;i++){
        gpio.port=101+i;
        if(hal_gpio_init(&gpio)!=0){
            perr("board io %d DO init error ~",i);
            sta=-1;
        }
        fun(&gpio);
    }
    gpio.config=INPUT_HIGH_IMPEDANCE;
    gpio.priv=NULL;
	for(int i=0;i<4;i++){
		gpio.port=107+i;
		if(hal_gpio_init(&gpio)!=0){
            perr("board io %d DI init error ~",i);
            sta=-1;
        }
	}
    xp_cli_debug_add(xp_boardIO_debug);
    println("board io DO init success");
    return 0;
}


/*
set board io state
param:
    pin: io pin number,1~4
    sta:0=low,1=hight
return:
    0=success
    -1= error
*/
int xp_boardIO_set(int pin,int sta){
    gpio_dev_t gpio;
    int32_t (*fun)(gpio_dev_t *);

    if(pin<1||pin>4){
        perr("pin number set fault!");
        return -1;
    }
    if(sta==0){
        fun=hal_gpio_output_low;
    }
    else{
        fun=hal_gpio_output_high;
    }
    gpio.port=pin+100;
    gpio.config=OUTPUT_PUSH_PULL;
    gpio.priv=NULL;

    return fun(&gpio);
}


/*
get board io state
param:
    pin: io pin number,1-4
return:
    pin state,0 or 1
    -1 = fail or error
*/
int xp_boardIO_get(int pin){
    gpio_dev_t gpio;
    gpio.port=pin+106;
    u32 value;
    int sta=0;

    sta=hal_gpio_input_get(&gpio,&value);
    if(sta==-1){
        return -1;
    }
    return (int)value;
}


/*
reset io board power
*/
void xp_boardIO_reset_pwr(void){
    xp_boardIO_set(1,1);
    aos_msleep(1000);
    xp_boardIO_set(1,0);
    // aos_msleep(2000);
}

/*
io out test
*/
void xp_boardIO_test(void){
    xp_boardIO_set(1,1);
    xp_boardIO_set(2,1);
    xp_boardIO_set(3,1);
    xp_boardIO_set(4,1);
    for(int i=1;i<=4;i++){
        xp_boardIO_set(i,0);
        if(i>1){
            xp_boardIO_set(i-1,1);
        }
        aos_msleep(300);
    }
    xp_boardIO_set(4,1);
}


/*
io exp chip reset
*/
void io_exp_rst(void){
    gpio_dev_t io_rst={0},io_rst_old={0};

    io_rst.port=4;
    io_rst.config=OUTPUT_PUSH_PULL;
    io_rst.priv=NULL;
    io_rst_old.port=1;
    io_rst_old.config=OUTPUT_PUSH_PULL;
    io_rst_old.priv=NULL;
    hal_gpio_init(&io_rst_old);

    hal_gpio_output_low(&io_rst);
    hal_gpio_output_low(&io_rst_old);
    aos_msleep(100);
    hal_gpio_output_high(&io_rst);
    hal_gpio_output_high(&io_rst_old);
    println("Rst io exp chip !");
}

void io_exp_reinit(void);
/*
io exp chip restore
*/
void io_restore(void){
    gpio_dev_t io_4g={0};

    io_4g.port=PORT_GPIO_4G_EN;
    io_4g.config=OUTPUT_PUSH_PULL;
    io_4g.priv=NULL;

    io_exp_reinit();
    println("Reinit io exp chip !");
    aos_msleep(100);
    xp_boardIO_init(1);
    hal_gpio_init(&io_4g);
    hal_gpio_output_high(&io_4g);
    println("Power on 4G module !");
    xp_boardIO_set(1,0);
}


/*
4g 断电重启
*/
void lte_power_rst(void){
    gpio_dev_t st_4g_en = {0};
    st_4g_en.port = PORT_GPIO_4G_EN;
    st_4g_en.config = OUTPUT_PUSH_PULL;
    st_4g_en.priv = NULL;

    hal_gpio_output_low(&st_4g_en);
    aos_msleep(500);
    hal_gpio_output_high(&st_4g_en);
}



/**************************************************debug*****************************/
//io debug
static int xp_boardIO_debug(char *type,char *fun,char *param){
    int i=0;

    if(strcmp(type,"boardIO")!=0)return 0;
    if(strcmp(fun,"set")==0){
        i=atoi(param);
        xp_boardIO_set(i,1);
    }
    else if(strcmp(fun,"reset")==0){
        i=atoi(param);
        xp_boardIO_set(i,0);
    }
    else if(strcmp(fun,"read")==0){
        i=atoi(param);
        println("board io %d state: %d.",i,xp_boardIO_get(i));
    }
    else if(strcmp(fun,"rst")==0){
        io_exp_rst();
    }
    else if(strcmp(fun,"restore")==0){
        io_restore();
    }
    else if(strcmp(fun,"rst 4g")==0){
        lte_power_rst();
    }
    else{
        return 0;
    }
    return 1;
}

//end of the file
